//
//  InviteCodeViewController.m
//  Cat
//
//  Created by 卢仕彤 on 2019/3/15.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "InviteCodeViewController.h"
#import "HttpUserManager.h"
#import "InviteListViewController.h"
#import "FLProgressHUD.h"

@interface InviteCodeViewController ()

@property (nonatomic, strong) UITextField *inviteCodeFiled;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *notInviteButton;
@property (nonatomic, strong) UIButton *quickLoginButton;
@property (nonatomic, strong) NSString *inviteCode;

@end

@implementation InviteCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.topTitleView setDetailDic:@{Nav_Title:@"邀请码"}.mutableCopy];
    [self loadAllSubViews];
}

- (void)loadAllSubViews {
    [self.view addSubviews:@[self.inviteCodeFiled,self.bottomView,self.notInviteButton,self.quickLoginButton]];
    
    [self setPlaceholder:@"请输入验证码" andWith:self.inviteCodeFiled];
    
    [self.inviteCodeFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(28.0));
        make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(28.0));
        make.top.equalTo(self.topTitleView.mas_bottom).offset(kQKFitModuleDecimal(40.0));
        make.height.equalTo(@(kQKFitModuleDecimal(30.0)));
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.inviteCodeFiled.mas_bottom).offset(kQKFitModuleDecimal(5.0));
        make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(28.0));
        make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(28.0));
        make.height.equalTo(@(1));
    }];
    
    [self.notInviteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.bottomView);
        make.top.equalTo(self.bottomView.mas_bottom).offset(kQKFitModuleDecimal(36.0));
        make.height.equalTo(@(kQKFitModuleDecimal(40.0)));
    }];
    
    [self.quickLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.notInviteButton);
        make.top.equalTo(self.notInviteButton.mas_bottom).offset(kQKFitModuleDecimal(27.0));
        make.height.equalTo(@(kQKFitModuleDecimal(40.0)));
    }];
}

- (void)setPlaceholder:(NSString *)placeholder andWith:(UITextField *)textFiled {
    NSDictionary *atts = @{NSForegroundColorAttributeName: COLOR_WITH_HEX(0xCDC7C4),
                           NSFontAttributeName: textFiled.font};
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:placeholder attributes:atts];
    textFiled.attributedPlaceholder = attStr;
}


#pragma mark 点击的事件

- (void)pushInviteListAction {
    InviteListViewController *inviteList = [[InviteListViewController alloc] init];
    inviteList.phoneString  = self.phoneString;
    inviteList.verCodeString = self.verCodeString;
    [self.navigationController pushViewController:inviteList animated:YES];
}

-  (void)quickLoginAction {
    
    if (self.inviteCodeFiled.text.length <= 0) {
        [FLProgressHUD showError:@"请输入正确的验证码"];
        return;
    }
    
    [HttpUserManager loginWithPhoneNumber:self.phoneString andVerifyCode:self.verCodeString inviteCode:self.inviteCodeFiled.text withSuccuss:^(NSString * _Nonnull token) {
        UDToken = token;
        [self.navigationController popToRootViewControllerAnimated:YES];
    } failure:^(NSInteger code, NSString * _Nonnull message) {
        [FLProgressHUD showError:message];
    }];
}

- (void)textFieldValueChanged:(UITextField *)textField {
    if ([textField isEqual:self.inviteCodeFiled]) {
        if (textField.text.length > 0) {
            self.quickLoginButton.enabled = YES;
        } else {
            self.quickLoginButton.enabled = NO;
        }
    }
}
#pragma mark lazy init

- (UITextField *)inviteCodeFiled {
    if (!_inviteCodeFiled) {
        _inviteCodeFiled = [[UITextField alloc] init];
        _inviteCodeFiled.keyboardType = UIKeyboardTypeNumberPad;
        [_inviteCodeFiled addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _inviteCodeFiled;
}

- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = COLOR_WITH_HEX(0xEEEBEB);
    }
    return _bottomView;
}

- (UIButton *)notInviteButton {
    if (!_notInviteButton) {
        _notInviteButton = [UIButton buttonWithTitle:@"没有邀请码" color:COLOR_WITH_HEX(0xFFC236) font:[UIFont systemFontOfSize:kQKFitFont(16)]];
        _notInviteButton.layer.cornerRadius = kQKFitModuleDecimal(6.0);
        _notInviteButton.layer.borderWidth = kQKFitModuleDecimal(1.0);
        _notInviteButton.layer.borderColor = COLOR_WITH_HEX(0xFFC236).CGColor;
        _notInviteButton.layer.masksToBounds = YES;
        [_notInviteButton addTarget:self action:@selector(pushInviteListAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _notInviteButton;
}

- (UIButton *)quickLoginButton {
    if (!_quickLoginButton) {
        _quickLoginButton = [UIButton buttonWithTitle:@"登录" color:COLOR_WITH_HEX(0x756D68) font:[UIFont systemFontOfSize:kQKFitFont(16)]];
        UIImage *nmImage = [UIImage imageWithColor:COLOR_WITH_HEX(0xFFC236)];
        UIImage *hlImage = [nmImage addBlackMask];
        UIImage *disImage = [UIImage imageWithColor:COLOR_WITH_HEX(0xCDC7C4)];
        
        _quickLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_quickLoginButton setTitle:@"登录" forState:UIControlStateNormal];
        
        [_quickLoginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_quickLoginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_quickLoginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        
        [_quickLoginButton setBackgroundImage:nmImage forState:UIControlStateNormal];
        [_quickLoginButton setBackgroundImage:hlImage forState:UIControlStateSelected];
        [_quickLoginButton setBackgroundImage:disImage forState:UIControlStateDisabled];
        _quickLoginButton.layer.cornerRadius = kQKFitModule(5);
        _quickLoginButton.layer.masksToBounds = YES;
        _quickLoginButton.enabled = NO;
        [_quickLoginButton addTarget:self action:@selector(quickLoginAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _quickLoginButton;
}

@end
